package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.dao.IBaseDao;

/**
 * dao的父类
 * 
 * @author Administrator
 *
 */
public class BaseDao implements IBaseDao {
	protected Connection con = null;
	protected PreparedStatement ps = null;
	protected ResultSet rs = null;

	/**
	 * 构造方法获得连接
	 */
	public BaseDao(int type) {
		if(type==0){
			con = DBHelper.getConnection();
			try {
				con.setAutoCommit(false);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#getCon()
	 */
	@Override
	public Connection getCon() {
		return con;
	}

	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#setCon(java.sql.Connection)
	 */
	@Override
	public void setCon(Connection con) {
		this.con = con;
	}




	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#commit()
	 */
	@Override
	public void commit(){
		try {
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#rollback()
	 */
	@Override
	public void rollback(){
		try{
			con.rollback();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#close()
	 */
	@Override
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IBaseDao#closePs()
	 */
	@Override
	public void closePs() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
